package com.seari.custody.controller;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.seari.custody.bean.Message;
import com.seari.custody.util.JwtTokenUtils;
import com.seari.custody.pojo.MessageBean;
import com.seari.custody.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/messages")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @GetMapping("/unReads")
    public MessageBean personalMeetingStatistics(@RequestHeader(value = JwtTokenUtils.TOKEN_HEADER, defaultValue = "") String token) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(token), "token不能为空");
        String userNumber = JwtTokenUtils.getUserNumber(token.substring(JwtTokenUtils.TOKEN_PREFIX.length()));
        int count = messageService.statisticsUnReadMessageByUserNumber(userNumber).size();

        return MessageBean.success("获取消息通知成功", count);
    }

    @PostMapping(value = "/updateMessage")
    public MessageBean updateMessage(Message message) {
        Integer integer = messageService.updateMessage(message);

        return MessageBean.success("更新消息成功", integer);
    }
}
